package com.wm.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.common.utils.StringUtils;
import com.wm.system.domain.CarInfo;
import com.wm.system.domain.CarPicture;
import com.wm.system.mapper.CarInfoMapper;
import com.wm.system.service.ICarInfoService;
import com.wm.system.service.ICarPictureService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 车辆信息 服务实现类
 * </p>
 *
 * @author hyq
 * @since 2023-09-26 21:47:23
 */
@Service
public class CarInfoServiceImpl extends ServiceImpl<CarInfoMapper, CarInfo> implements ICarInfoService {

    @Autowired
    private ICarPictureService carPictureService;

    @Override
    public List<CarInfo> selectCaeInfoList(CarInfo carInfo) {
        QueryWrapper<CarInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(carInfo.getBrandId()!=null, CarInfo::getBrandId,carInfo.getBrandId())//车辆品牌
                .eq(StringUtils.isNotEmpty(carInfo.getCarLocation()), CarInfo::getCarLocation,carInfo.getCarLocation())//车源地
                .eq(carInfo.getCarAge() != null, CarInfo::getCarAge,carInfo.getCarAge())//车龄
                .eq(carInfo.getCarPrice()!=null, CarInfo::getCarPrice,carInfo.getCarPrice())//车价格
                .eq(carInfo.getMileage()!=null, CarInfo::getMileage,carInfo.getMileage())//里程数（万公里）
                .eq(StringUtils.isNotEmpty(carInfo.getTransmission()), CarInfo::getTransmission,carInfo.getTransmission())//变速箱
                .eq(StringUtils.isNotEmpty(carInfo.getCarArchitecture()), CarInfo::getCarArchitecture,carInfo.getCarArchitecture())//车身架构
                .eq(StringUtils.isNotEmpty(carInfo.getEmissionStandard()), CarInfo::getEmissionStandard,carInfo.getEmissionStandard())//排放标准
                .eq(StringUtils.isNotEmpty(carInfo.getEnergyType()), CarInfo::getEnergyType,carInfo.getEnergyType())//能源类型
                .eq(StringUtils.isNotEmpty(carInfo.getDisplacement()), CarInfo::getDisplacement,carInfo.getDisplacement())//排量
                .eq(StringUtils.isNotEmpty(carInfo.getSeat()), CarInfo::getSeat,carInfo.getSeat())//座椅
                .eq(carInfo.getChoiceCar()!=null, CarInfo::getChoiceCar,carInfo.getChoiceCar());//1：精选车源，2：非精选车源
        if(carInfo.getGroundingTime()!=null){ //上架时间
            wrapper.lambda().orderByDesc(CarInfo::getGroundingTime);
        }
        if (StringUtils.isNotEmpty(carInfo.getCarAgeType())){
            wrapper.lambda().orderByAsc(CarInfo::getCarAge);
        }
        if (StringUtils.isNotEmpty(carInfo.getMileageType())){
            wrapper.lambda().orderByAsc(CarInfo::getMileage);
        }
        if (StringUtils.isNotEmpty(carInfo.getCarPriceType())&&carInfo.getCarPriceType().equals("1")){
            wrapper.lambda().orderByAsc(CarInfo::getCarPrice);
        }
        if(StringUtils.isNotEmpty(carInfo.getCarPriceType())&&carInfo.getCarPriceType().equals("2")){
            wrapper.lambda().orderByDesc(CarInfo::getCarPrice);
        }
        List<CarInfo> list = this.list(wrapper);
        return list;
    }

    @Override
    public void addOrUpdate(CarInfo carInfo) throws Exception {
        if(Objects.isNull(carInfo)){
            throw new Exception("数据不可为空");
        }
        if(carInfo.getId()==null){
            carInfo.setGrounding(1);
            carInfo.setGroundingTime(LocalDateTime.now());
            carInfo.setCreateTime(LocalDateTime.now());
            this.save(carInfo);
            ArrayList<CarPicture> list = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(carInfo.getFileUrlList())){
                carInfo.getFileUrlList().forEach(e->{
                    CarPicture carPicture = new CarPicture();
                    carPicture.setFileUrl(e);
                    carPicture.setCreatTime(LocalDateTime.now());
                    carPicture.setCarId(carInfo.getId());
                    list.add(carPicture);
                });
                carPictureService.saveBatch(list);
            }
        }else {
            this.updateById(carInfo);
        }
    }

    @Override
    public CarInfo getInfo(CarInfo carInfo) {
        CarInfo byId = this.getById(carInfo.getId());
        return byId;
    }


}
